//---------------------------------------------------------------------------


#pragma hdrstop

#include "class_TabCtrl.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
__fastcall TTabCtrl::TTabCtrl()
{
 list.clear();
 Ctrl = false;
}
__fastcall TTabCtrl::~TTabCtrl()
{
 // all del
 for(std::list<TTabItem*>::const_iterator i = list.begin(); i!= list.end(); ++i)
 {
  delete (*i);
 }
 list.clear();
}
void __fastcall TTabCtrl::Add( TWinControl *Item, TWinControl *Up, TWinControl *Down, TWinControl *Left, TWinControl *Right, TWinControl *Next, TWinControl *Prev)
{
 TTabItem *I = new TTabItem();
 I->Item = Item;
 I->Up = Up;
 I->Down = Down;
 I->Left = Left;
 I->Right = Right;
 I->Next = Next;
 I->Prev = Prev;
 list.push_back(I);
}
void __fastcall TTabCtrl::Process(TForm *form, tagMSG &Msg, bool &Handled)
{
 if(Msg.message == WM_KEYUP)
 {
  if(Msg.wParam == keyCtrl) Ctrl = false;
 }
 if(Msg.message == WM_KEYDOWN)
 {
  if(Msg.wParam == keyCtrl) Ctrl = true;
  if(!Ctrl)return;
  for(std::list<TTabItem*>::const_iterator i = list.begin(); i!= list.end(); ++i)
  {
   if( (*i)->Item == form->ActiveControl )
   {
    switch(Msg.wParam)
    {
     case keyUp:Focuse((*i)->Up, Msg.wParam);break;
     case keyDown:Focuse((*i)->Down, Msg.wParam);break;
     case keyLeft:Focuse((*i)->Left, Msg.wParam);break;
     case keyRight:Focuse((*i)->Right, Msg.wParam);break;
    }
    Handled = InKeys(Msg.wParam);
    return;
   }
  }
 }
 
}

void __fastcall TTabCtrl::Focuse(TWinControl * I, int Key)
{
   if( I ==  NULL) return;
   // test na visible and Enabled
   if (I->Visible&&I->Enabled)
   {
    // set focuse
    I->SetFocus();
   }
   else
   {
    // search
    for(std::list<TTabItem*>::const_iterator i = list.begin(); i!= list.end(); ++i)
    {
     if( (*i)->Item == I)
     {
      switch(Key)
      {
       case keyUp: Focuse((*i)->Prev, Key);break;
       case keyLeft: Focuse((*i)->Prev, Key);break;
       case keyDown: Focuse((*i)->Next, Key);break;
       case keyRight: Focuse((*i)->Next, Key);break;
      }
      return;
     } 
    }
   }
}
bool __fastcall TTabCtrl::InKeys(int Key)
{
 if ((Key == keyUp)||(Key == keyDown)||(Key == keyLeft)||(Key == keyRight)||(Key == keyCtrl))
 {
  return true;
 }
 else
 {
  return false;
 }
}
void __fastcall TTabCtrl::Next(TForm *form)
{

}

